﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace BusLib
{
    public partial class Route
    {
        public IEnumerable<Trip> InServiceTrips
        {
            get
            {
                return Trips.Where(t => t.Stop_Times.Any());
            }
        }

        internal void ProcessReport(AvlService.AvlReport[] report)
        {
            System.Collections.Generic.HashSet<Bus> oldBuses = new System.Collections.Generic.HashSet<Bus>();
            foreach (Bus b in Buses)
            {
                oldBuses.Add(b);
            }

            // Update all buses on the route:
            foreach (AvlService.AvlReport r in report)
            {
                Bus b = BusLib.Bus.GetBusByNumber(r.vehicleID);
                oldBuses.Remove(b);
                b.Update(r);
            }

            // Delete the buses that are no longer on this route
            foreach (Bus b in oldBuses)
            {
                b.Route = null;
            }
        }

        public void Update()
        {
            AvlService.AvlService svc = new AvlService.AvlService();

            AvlService.AvlReport[] report;
            while (true)
            {
                // if at first you don't succeed,
                try // try again
                {
                    report = svc.getLatestByRoute("http://transit.metrokc.gov", int.Parse(route_short_name));
                    break;
                }
                catch (System.Net.WebException)
                {
                }
            }

            if (report != null)
                ProcessReport(report);
        }

        static public Route ImportCSV(string line)
        {
            Route route = null;
            string[] fields = line.Split(',');

            if (fields[0] != "route_id")
            {
                route = new Route();

                route.route_id = int.Parse(fields[0]);
                route.agency_id = fields[1];
                route.route_short_name = fields[2];
                route.route_long_name = fields[3];
                route.route_desc = fields[4];
                route.route_type = int.Parse(fields[5]);
                route.route_url = fields[6];
                if (fields[7] != "")
                {
                    route.route_color = int.Parse(fields[7]);
                }
                if (fields[8] != "")
                {
                    route.route_text_color = int.Parse(fields[8]);
                }

                BusLibDataContext.db.Routes.InsertOnSubmit(route);
                BusLibDataContext.db.SubmitChanges();
            }
            return route;
        }

        static public Route GetRouteByNumber(int route_num)
        {
            // Try to find it in the DB:
            BusLibDataContext db = BusLibDataContext.db;

            return db.Routes.Single(r => r.route_short_name == route_num.ToString());
        }

    }
}
